<template>
  <div class="home-container">
    <div class="header">
      <h1>待办事项列表</h1>
      <div class="actions">
        <button @click="handleLogout" class="logout-btn">
          <i class="icon-logout"></i> 退出
        </button>
      </div>
    </div>

    <nav class="main-nav">
      <router-link to="/" class="nav-link" active-class="active">
        <i class="icon-home"></i> 首页
      </router-link>
      <router-link to="/stats" class="nav-link" active-class="active">
        <i class="icon-stats"></i> 数据
      </router-link>
      <router-link to="/about" class="nav-link" active-class="active">
        <i class="icon-about"></i> 关于
      </router-link>
    </nav>

    <div class="todo-app">
      <div class="input-section">
        <TodoForm />
        <div class="search-box">
          <input 
            type="text" 
            v-model="searchQuery" 
            placeholder="搜索待办事项..."
            class="search-input"
          >
          <i class="icon-search"></i>
        </div>
      </div>

      <TodoFilter class="filter-section" />

      <ul class="todo-list" v-if="filteredTodos.length > 0">
        <TodoItem 
          v-for="todo in filteredTodos" 
          :key="todo.id" 
          :todo="todo"
          @toggle="toggleTodo"
          @delete="deleteTodo"
        />
      </ul>
      <div v-else class="empty-state">
        <i class="icon-empty"></i>
        <p>暂无待办事项</p>
      </div>

      <button 
        v-if="stats.completed > 0" 
        @click="clearCompleted" 
        class="clear-btn"
      >
        <i class="icon-clear"></i> 清除已完成
      </button>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { storeToRefs } from 'pinia'
import { useTodoStore } from '../stores/todo'
import { useAuthStore } from '../stores/auth'
import { useRouter } from 'vue-router'
import TodoItem from '../components/TodoItem.vue'
import TodoForm from '../components/TodoForm.vue'
import TodoFilter from '../components/TodoFilter.vue'

const router = useRouter()
const authStore = useAuthStore()
const todoStore = useTodoStore()

const { filteredTodos, stats } = storeToRefs(todoStore)
const { toggleTodo, deleteTodo, clearCompleted } = todoStore
const searchQuery = ref('')

const handleLogout = () => {
  authStore.logout()
  router.push('/login')
}
</script>

<style scoped>
.home-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 2rem;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 2rem;
}

.header h1 {
  color: #2c3e50;
  font-size: 2rem;
  margin: 0;
}

.main-nav {
  display: flex;
  gap: 1.5rem;
  margin-bottom: 2rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid #eee;
}

.nav-link {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  color: #666;
  text-decoration: none;
  font-weight: 500;
  padding: 0.5rem 1rem;
  border-radius: 6px;
  transition: all 0.3s ease;
}

.nav-link:hover {
  color: #42b983;
  background: #f5f5f5;
}

.nav-link.active {
  color: #42b983;
  background: rgba(66, 185, 131, 0.1);
}

.todo-app {
  background: white;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  padding: 2rem;
}

.input-section {
  display: flex;
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.search-box {
  position: relative;
  flex: 1;
}

.search-input {
  width: 100%;
  padding: 0.75rem 1rem 0.75rem 2.5rem;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 1rem;
}

.search-input:focus {
  outline: none;
  border-color: #42b983;
}

.icon-search {
  position: absolute;
  left: 1rem;
  top: 50%;
  transform: translateY(-50%);
  color: #999;
}

.filter-section {
  margin-bottom: 1.5rem;
}

.todo-list {
  list-style: none;
  padding: 0;
  margin: 0 0 1.5rem 0;
}

.empty-state {
  text-align: center;
  padding: 2rem;
  color: #999;
}

.empty-state i {
  font-size: 3rem;
  margin-bottom: 1rem;
  display: block;
  color: #ddd;
}

.clear-btn {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-top: 1rem;
  padding: 0.75rem 1.5rem;
  background-color: #ff6b6b;
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 1rem;
  transition: background 0.3s;
}

.clear-btn:hover {
  background-color: #ff5252;
}

.logout-btn {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 1rem;
  background: rgba(255, 107, 107, 0.1);
  color: #ff6b6b;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 500;
  transition: all 0.3s;
}

.logout-btn:hover {
  background: rgba(255, 107, 107, 0.2);
}
</style>